﻿using Company.Core;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace Company.Application.Station.Converters
{
    public class ProtocalToTextConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return "异常";
            }

            if(int.TryParse(value.ToString(),out int result))
            {
                ProtocolType protocolType = (ProtocolType)result;
                return protocolType.ToString();
            }

            return "异常";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //尝试转换
            if(Enum.TryParse<ProtocolType>(value.ToString(),out ProtocolType protocol))
            {
                return (int)protocol;
            }
            return value;
        }
    }
}
